<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/20
 * Time: 03:12
 */

namespace App\Http\Services;


use App\Http\Models\User;
use Symfony\Component\Finder\Exception\AccessDeniedException;

class UserService
{

    public static function getByUsernameAndPassword($username, $password)
    {
        $user = User::where('username', $username)->where('password', $password)->where('is_del', 0)->first();

        if (!$user) {
            return [];
        }

        return [
            'id' => $user->id,
            'username' =>$user->username,
            'realname' => $user->realname,
            'gender' => $user->gender,
            'identity' => $user->identity,
            'number' => $user->number,
            'mobile' => $user->mobile,
            'email' => $user->email,
            'university' => $user->university,
            'college' => $user->college,
            'enroll' => $user->enroll,
            'role' => $user->role
        ];
    }

    public static function getByUsernameAndEmail($username, $email)
    {
        $user = User::where('username', $username)->where('email', $email)->where('is_del', 0)->first();

        if (!$user) {
            return [];
        }

        return [
            'id' => $user->id,
            'username' =>$user->username,
            'realname' => $user->realname,
            'gender' => $user->gender,
            'identity' => $user->identity,
            'number' => $user->number,
            'mobile' => $user->mobile,
            'email' => $user->email,
            'university' => $user->university,
            'college' => $user->college,
            'enroll' => $user->enroll,
            'role' => $user->role
        ];
    }

    public static function getById($id)
    {
        $user = User::where('id', $id)->where('is_del', 0)->first();

        if (!$user) {
            return [];
        }

        return [
            'id' => $user->id,
            'username' =>$user->username,
            'realname' => $user->realname,
            'gender' => $user->gender,
            'identity' => $user->identity,
            'number' => $user->number,
            'mobile' => $user->mobile,
            'email' => $user->email,
            'university' => $user->university,
            'college' => $user->college,
            'enroll' => $user->enroll,
            'role' => $user->role
        ];
    }

    public static function getList($limit, $offset, $order = 'desc')
    {
        $query = User::where('is_del', 0);
        $total = $query->count();
        $users = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($users as $user) {
            $data[] = [
                'id' => $user->id,
                'username' => $user->username,
                'realname' => $user->realname,
                'number' => $user->number,
                'email' => $user->email,
                'role' => $user->role,
                'university' => $user->university,
                'college' => $user->college
            ];
        }

        return [
            'total' => $total,
            'rows' => $data,
            'page_size' => $limit,
            'offset' => $offset,
            'pages' => ceil($total / $limit),
            'page_number' => $offset / $limit + 1
        ];
    }

    public static function save($data)
    {
        $now = date('Y-m-d H:i:s', time());
        if (array_key_exists('id', $data)) {
            $user = User::where('id', $data['id'])->where('is_del', 0)->first();
            if (!$user) {
                throw new AccessDeniedException('用户不存在或已删除!', 4032);
            }
        } else {
            $user = new User();
            $user->is_del = 0;
            $user->created_at = $now;
        }
        foreach ($data as $key => $value) {
            $user->$key = $value;
        }
        $user->updated_at = $now;
        $user->save();
    }

    public static function delete($id)
    {
        $user = User::where('id', $id)->where('is_del', 0)->first();
        if (!$user) {
            throw new AccessDeniedException('用户不存在或已删除', 4032);
        }
        $user->is_del = 1;
        $user->updated_at = date('Y-m-d H:i:s', time());
        $user->save();
    }

    public static function changePass($data)
    {
        $user = User::where('id', $data['id'])->where('is_del', 0)->first();
        if (!$user) {
            throw new AccessDeniedException('用户不存在或已删除!', 4032);
        }
        if ($user->password != $data['old_pass']) {
            throw new AccessDeniedException('原密码不正确!', 4032);
        }
        if ($data['new_pass'] != $data['new_pass_sec']) {
            throw new AccessDeniedException('两次密码不一致!', 4032);
        }
        $user->password = $data['new_pass'];
        $user->updated_at = date('Y-m-d H:i:s', time());
        $user->save();
    }

    public static function register($data)
    {
        $user = User::where('username', $data['username'])->first();
        if ($user) {
            throw new AccessDeniedException('用户已存在!', 4032);
        }
        if ($data['password'] != $data['re_password']) {
            throw new AccessDeniedException('两次密码不一致!', 4032);
        }
        $user = new User();
        $user->username = $data['username'];
        $user->password = $data['password'];
        $user->mobile = $data['mobile'];
        $user->email = $data['email'];
        $user->role = 1;
        $now = date('Y-m-d H:i:s', time());
        $user->created_at = $now;
        $user->updated_at = $now;
        $user->save();
        return [
            'id' => $user->id,
            'username' =>$user->username,
            'realname' => $user->realname,
            'gender' => $user->gender,
            'identity' => $user->identity,
            'number' => $user->number,
            'mobile' => $user->mobile,
            'email' => $user->email,
            'university' => $user->university,
            'college' => $user->college,
            'enroll' => $user->enroll,
            'role' => $user->role
        ];
    }
}